"""
知识库相关的数据传输对象(DTO)定义
"""
from datetime import datetime
from typing import Optional, List
from pydantic import BaseModel, Field
from bson import ObjectId
from app.models.base import PyObjectId
from app.models.knowledge import (
    FileType,
    FileFormat,
    FilePermission
)
from app.models.knowledge_folder import FolderPermission


class KnowledgeFileCreate(BaseModel):
    """创建知识库文件模型"""
    title: str = Field(..., min_length=1, max_length=200, description="文件标题")
    description: Optional[str] = Field(None, max_length=1000, description="文件描述")
    folder_id: Optional[str] = Field(None, description="所属文件夹ID")
    project_id: Optional[str] = Field(None, description="关联项目ID（可选）")
    tags: List[str] = Field(default_factory=list, description="标签列表")
    permission: FilePermission = Field(default=FilePermission.PRIVATE, description="权限")
    is_public: bool = Field(default=False, description="是否公开")


class KnowledgeFileUpdate(BaseModel):
    """更新知识库文件模型"""
    title: Optional[str] = Field(None, min_length=1, max_length=200, description="文件标题")
    description: Optional[str] = Field(None, max_length=1000, description="文件描述")
    folder_id: Optional[str] = Field(None, description="所属文件夹ID")
    project_id: Optional[str] = Field(None, description="关联项目ID（可选）")
    tags: Optional[List[str]] = Field(None, description="标签列表")
    permission: Optional[FilePermission] = Field(None, description="权限")
    is_public: Optional[bool] = Field(None, description="是否公开")


class KnowledgeFileResponse(BaseModel):
    """知识库文件响应模型"""
    id: str = Field(..., description="文件ID")
    title: str = Field(..., description="文件标题")
    filename: str = Field(..., description="原始文件名")
    file_path: str = Field(..., description="MinIO存储路径")
    file_size: int = Field(..., description="文件大小（字节）")
    mime_type: str = Field(..., description="MIME类型")
    file_type: str = Field(..., description="文件类型")
    format: str = Field(..., description="文件格式")
    folder_id: Optional[str] = Field(None, description="所属文件夹ID")
    folder_name: Optional[str] = Field(None, description="文件夹名称")
    tags: List[str] = Field(default_factory=list, description="标签列表")
    description: Optional[str] = Field(None, description="文件描述")
    project_id: Optional[str] = Field(None, description="关联项目ID")
    project_name: Optional[str] = Field(None, description="项目名称")
    author_id: str = Field(..., description="创建者ID")
    author_name: Optional[str] = Field(None, description="创建者名称")
    organization_id: Optional[str] = Field(None, description="组织ID")
    is_public: bool = Field(..., description="是否公开")
    permission: str = Field(..., description="权限")
    download_count: int = Field(..., description="下载次数")
    view_count: int = Field(..., description="查看次数")
    favorite_count: int = Field(..., description="收藏次数")
    status: str = Field(..., description="状态")
    created_at: datetime = Field(..., description="创建时间")
    updated_at: datetime = Field(..., description="更新时间")
    is_favorited: Optional[bool] = Field(None, description="当前用户是否已收藏")


class KnowledgeFileSummary(BaseModel):
    """知识库文件摘要模型"""
    id: str = Field(..., description="文件ID")
    title: str = Field(..., description="文件标题")
    filename: str = Field(..., description="原始文件名")
    file_size: int = Field(..., description="文件大小（字节）")
    file_type: str = Field(..., description="文件类型")
    format: str = Field(..., description="文件格式")
    author_name: Optional[str] = Field(None, description="创建者名称")
    created_at: datetime = Field(..., description="创建时间")


class KnowledgeFolderCreate(BaseModel):
    """创建知识库文件夹模型"""
    name: str = Field(..., min_length=1, max_length=100, description="文件夹名称")
    parent_id: Optional[str] = Field(None, description="父文件夹ID")
    project_id: Optional[str] = Field(None, description="关联项目ID（可选）")
    description: Optional[str] = Field(None, max_length=500, description="文件夹描述")
    permission: FolderPermission = Field(default=FolderPermission.PRIVATE, description="权限")


class KnowledgeFolderUpdate(BaseModel):
    """更新知识库文件夹模型"""
    name: Optional[str] = Field(None, min_length=1, max_length=100, description="文件夹名称")
    parent_id: Optional[str] = Field(None, description="父文件夹ID")
    description: Optional[str] = Field(None, max_length=500, description="文件夹描述")
    permission: Optional[FolderPermission] = Field(None, description="权限")


class KnowledgeFolderResponse(BaseModel):
    """知识库文件夹响应模型"""
    id: str = Field(..., description="文件夹ID")
    name: str = Field(..., description="文件夹名称")
    parent_id: Optional[str] = Field(None, description="父文件夹ID")
    project_id: Optional[str] = Field(None, description="关联项目ID")
    project_name: Optional[str] = Field(None, description="项目名称")
    description: Optional[str] = Field(None, description="文件夹描述")
    author_id: str = Field(..., description="创建者ID")
    author_name: Optional[str] = Field(None, description="创建者名称")
    organization_id: Optional[str] = Field(None, description="组织ID")
    permission: str = Field(..., description="权限")
    file_count: int = Field(..., description="文件数量")
    created_at: datetime = Field(..., description="创建时间")
    updated_at: datetime = Field(..., description="更新时间")
    children: Optional[List['KnowledgeFolderResponse']] = Field(None, description="子文件夹列表")


# 前向引用解析
KnowledgeFolderResponse.model_rebuild()


class KnowledgeShareCreate(BaseModel):
    """创建文件分享模型"""
    allow_download: bool = Field(default=True, description="是否允许下载")
    expires_in_hours: Optional[int] = Field(None, ge=1, le=720, description="过期时间（小时），None表示永不过期")
    max_access_count: Optional[int] = Field(None, ge=1, description="最大访问次数")
    password: Optional[str] = Field(None, min_length=4, max_length=20, description="访问密码")


class KnowledgeShareResponse(BaseModel):
    """文件分享响应模型"""
    id: str = Field(..., description="分享ID")
    file_id: str = Field(..., description="文件ID")
    token: str = Field(..., description="分享令牌")
    share_url: str = Field(..., description="分享URL")
    allow_download: bool = Field(..., description="是否允许下载")
    expires_at: Optional[datetime] = Field(None, description="过期时间")
    access_count: int = Field(..., description="访问次数")
    max_access_count: Optional[int] = Field(None, description="最大访问次数")
    has_password: bool = Field(..., description="是否有密码")
    created_by: str = Field(..., description="创建者ID")
    created_at: datetime = Field(..., description="创建时间")
    is_active: bool = Field(..., description="是否有效")


class KnowledgeShareAccessResponse(BaseModel):
    """通过分享链接访问文件的响应模型"""
    file: KnowledgeFileResponse = Field(..., description="文件信息")
    share: KnowledgeShareResponse = Field(..., description="分享信息")


class KnowledgeCommentCreate(BaseModel):
    """创建文件评论模型"""
    content: str = Field(..., min_length=1, max_length=2000, description="评论内容")
    parent_id: Optional[str] = Field(None, description="父评论ID（回复）")


class KnowledgeCommentResponse(BaseModel):
    """文件评论响应模型"""
    id: str = Field(..., description="评论ID")
    file_id: str = Field(..., description="文件ID")
    user_id: str = Field(..., description="评论者ID")
    user_name: Optional[str] = Field(None, description="评论者名称")
    user_avatar: Optional[str] = Field(None, description="评论者头像")
    content: str = Field(..., description="评论内容")
    parent_id: Optional[str] = Field(None, description="父评论ID")
    created_at: datetime = Field(..., description="创建时间")
    updated_at: datetime = Field(..., description="更新时间")


class BatchDeleteRequest(BaseModel):
    """批量删除请求模型"""
    ids: List[str] = Field(..., min_items=1, description="要删除的文件ID列表")


class KnowledgeStats(BaseModel):
    """知识库统计信息模型"""
    total_files: int = Field(..., description="文件总数")
    total_folders: int = Field(..., description="文件夹总数")
    total_size: int = Field(..., description="总文件大小（字节）")
    files_by_type: dict = Field(default_factory=dict, description="按类型统计文件数")
    files_by_format: dict = Field(default_factory=dict, description="按格式统计文件数")
    recent_files: List[KnowledgeFileSummary] = Field(default_factory=list, description="最近文件")
    popular_files: List[KnowledgeFileSummary] = Field(default_factory=list, description="热门文件")

